namespace RSCore.Files;

public sealed record class ConfigFile(string Name) : RSFile(Name), IKeyConvertable, IExtensibleFile
{
	public readonly Dictionary<string, string> Text = new Dictionary<string, string>();

	public IDictionary<string, string> ExtendedProperties => Text;

	public IEnumerable<KeyValuePair<KeyData, string>> Convert()
	{
		foreach (var (property, text) in Text)
		{
			yield return new(new(Name, EntryType.Config, property), text);
		}
	}

	public override void Deserialize(JsonValue json)
	{
		Text.Clear();
		foreach (var (key, text) in json.Qo())
		{
			Text.Add(key, text.Qs());
		}
	}

	public override void Load(BinaryReader reader)
	{
		int count = reader.ReadInt32();
		for (int i = 0; i < count; i++)
		{
			Text.Add(reader.ReadString(), reader.ReadString());
		}
	}

	public override void Read(StreamReader reader)
	{

	}

	public override void Save(BinaryWriter writer)
	{
		writer.Write(Text.Count);
		foreach (var (key, text) in Text)
		{
			writer.Write(key);
			writer.Write(text);
		}
	}

	public override JsonValue Serialize()
	{
		return new JsonObject(Text.Select(s => new KeyValuePair<string, JsonValue>(s.Key, s.Value)));
	}

	public override void Write(StreamWriter writer, RSFile? annotation = null)
	{

	}
}